package org.formal.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.formal.common.controller.BaseController;
import org.formal.common.data.ResultData;
import org.formal.system.entity.RoleEntity;
import org.formal.system.service.RoleService;
import org.formal.system.shiro.manager.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色控制层
 * @author jiangyan
 *
 */
@RestController
@RequestMapping(value = "/role")
public class RoleController extends BaseController<RoleEntity> {
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 根据用户id获取角色集合 如为空 获取当前用户id
	 * @param userId 用户id
	 * @return 角色集
	 */
	@RequestMapping(value="/getRolesByUserId")
	public List<RoleEntity> getRolesByUserId(String userId){
		if(userId == null) {
			userId = TokenManager.getUserId();
		}
		List<RoleEntity> roleEntities = this.roleService.getRoleByUserId(userId);
		return roleEntities;
	}
	
	/**
	 * 根据userId 获取角色id字符串
	 * @param userId 角色id
	 * @return
	 */
	@RequestMapping(value="getRoleIdsByUserId")
	public String getRoleIdsByUserId(String userId) {
		if(userId == null) {
			userId = TokenManager.getUserId();
		}
		return this.roleService.getRoleIdsByUserId(userId);
	}
	
	/**
	 * 根据菜单id获取角色集合 
	 * 
	 */
	@RequestMapping(value="getRolesByResourceId")
	public List<RoleEntity> getRolesByResourceId(String ResourceId){
		return this.roleService.getRolesByResourceId(ResourceId);
	}
	
	/**
	 * 根据菜单id获取角色id集合
	 * 
	 */
	@RequestMapping(value="getRoleIdsByResourceId")
	public String getRoleIdsByResourceId(String resourceId){
		return this.roleService.getRoleIdsByResourceId(resourceId);
	}
	
	@RequestMapping(value="saveRoleRelation")
	public ResultData saveRoleRelation(String resourceId, String roleIds) {
		String [] roleArray = roleIds.split(",");
		this.roleService.saveRoleRelation(resourceId, roleArray);
		return null;
		
	}
	
	@RequestMapping(value="list")
	public List<RoleEntity> getList(String order, String sort, String param) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("isOpen", true);
		return super.getList(order, sort, map);
	}
}